package com.hrsoft.modules.bid.service.impl;

import com.hrsoft.modules.bid.dto.GoodsTreeDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrsoft.common.utils.PageUtils;
import com.hrsoft.common.utils.Query;

import com.hrsoft.modules.bid.dao.GoodsCategoryDao;
import com.hrsoft.modules.bid.entity.GoodsCategoryEntity;
import com.hrsoft.modules.bid.service.GoodsCategoryService;


@Service("goodsCategoryService")
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryDao, GoodsCategoryEntity> implements GoodsCategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<GoodsCategoryEntity> queryWrapper = new QueryWrapper<GoodsCategoryEntity>();
        if (params.get("key") != null && StringUtils.isNotBlank(params.get("key").toString())) {
            queryWrapper.like("name", "%" + params.get("key").toString() + "%");
        }
        if (params.get("parentId") != null && params.get("parentId").toString() != "0") {
            queryWrapper.eq("parent_id", params.get("parentId"));
        }
        IPage<GoodsCategoryEntity> page = this.page(
                new Query<GoodsCategoryEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<GoodsTreeDto> getTreeList (Integer parentId) {
        List<GoodsTreeDto> treeList = new ArrayList<>();
        QueryWrapper<GoodsCategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<GoodsCategoryEntity> goodsClassList = this.list(wrapper);
        GoodsTreeDto tmpGoodsTree = null;
        if (goodsClassList.size() <= 0) {
            return null;
        }
        for (GoodsCategoryEntity tmpgoodsClass : goodsClassList) {
            tmpGoodsTree = new GoodsTreeDto();
            tmpGoodsTree.setId(tmpgoodsClass.getId());
            tmpGoodsTree.setLabel(tmpgoodsClass.getName());
            tmpGoodsTree.setChildren(this.getTreeList(tmpgoodsClass.getId()));
            treeList.add(tmpGoodsTree);
        }
        return treeList;
    }

}